<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');

into::basic_class('admin');

class tag extends admin
{	
	public function nav2()
	{
		global $G;
		$nav = array();
		foreach($G['option']['type'] as $k=>$v){
			if(preg_match('/^(2|3|4|5)$/',$k)){
				$nav[$k] = array(
					'name' => $v,
					'mold' => 'tag',
					'param' => array('type'=>$k)
				);
			}
		}
		return $nav;
	}
	
	public function init()
	{
		global $G;
		$G['cover'] = $this->cover();
		$G['navs0'] = self::nav2();
		$type = arrExist($G['get'],'type');
		if(!$type){
			$G['get']['type'] = $type = key($G['navs0']);
		}
		$G['navs0'][$type]['active'] = true;
		$folder = arrExist(mysql::select_one('folder','items',"type='{$type}'",'sort DESC, id ASC'),'folder');
		$data = page::tag_pages($type,null,$G['rows']=$this->rows(20));
		$table = array_search($type,$G['pass']['type']);
		foreach($data['list'] as $k=>$v){
			if($v['parent']){
				$data['list'][$k]['parent'] = $parent = arrExist(mysql::select_one("GROUP_CONCAT(id SEPARATOR ',') AS parent",$table,"id IN ({$v['parent']})"),'parent');
				$data['list'][$k]['amount'] = $amount = arrExist(mysql::select_one("COUNT(*) AS amount",$table,"id IN ({$v['parent']}) AND display=1"),'amount');
				mysql::update(array('parent'=>$parent,'amount'=>$amount),'tag',"id='{$v['id']}'");
			}
			$data['list'][$k]['url'] = url::tag($folder, $v);
		}
		echo $this->theme('tag/tag', $data);
	}
	
	
	public function edit()
	{
		global $G;
		/* boss_CMS */
		$G['cover'] = $this->cover('tag');
		$data = array();
		foreach($G['option']['type'] as $k=>$v){
			if(preg_match('/^(2|3|4|5)$/',$k)){
				$G['type'][$k] = $v;
			}
		}
		if(isset($G['get']['id'])){
			$data = mysql::select_one('*','tag',"id='{$G['get']['id']}'");
		}
		echo $this->theme('tag/edit', $data);
	}
	
	public function add()
	{	
		global $G;
		$this->cover('tag',$G['get']['id']?'M':'A');
		if($title = arrExist($G['post'],'title')){
			if($id = $G['get']['id']){
				$data = array(
					'type'        => $G['get']['type'],
					'name'        => $G['post']['name'],
					'title'       => $title
				);
				if($result = mysql::select_one('id','tag',"id='{$id}'")){
					if(mysql::total("tag","title='{$data['title']}' AND type='{$G['get']['type']}' AND id!='{$id}'")){
						alert('标签名称已经存在');
					}
					if(preg_match('/^\d+$/',$data['name'])){
						alert('路径名称不能为纯数字');
					}
					if($data['name'] && mysql::total("tag","name='{$data['name']}' AND type='{$G['get']['type']}' AND id!='{$id}'")){
						alert('路径名称已经存在');
					}
					mysql::update($data,"tag","id='{$id}'");
				}
				alert('操作成功', url::mpf('tag','tag','edit',array('id'=>$id,'success'=>'ok')));
			}else{
				$has = array();
				$title = json::defilter($title);
				foreach($G['post']['type'] as $type){
					foreach($title as $v){
						if($v !== ''){
							$data = array(
								'type'        => $type,
								'name'        => '',
								'title'       => $v,
								'parent'      => '',
								'seo_title'   => '',
								'keywords'    => '',
								'description' => ''
							);
							if(mysql::total("tag","title='{$v}' AND type='{$type}'")){
								$has[] = $v;
							}else{
								$id = mysql::insert($data,"tag");
							}
						}
					}
				}
				alert('操作成功', url::mpf('tag','tag','edit',array('id'=>$id,'success'=>'ok')));
			}
		}else{
			alert('没有提交信息');
		}
	}

	
	public function seo()
	{
		global $G;
		$G['cover'] = $this->cover('tag');
		if(isset($G['get']['id'])){
			$data = mysql::select_one('*','tag',"id='{$G['get']['id']}'");
			echo $this->theme('tag/seo', $data);
		}
	}
	
	public function addseo()
	{	
		global $G;
		$this->cover('tag','M');
		if($G['post'] && $id = $G['get']['id']){
			$data = array(
				'seo_title'   => $G['post']['seo_title'],
				'keywords'    => $G['post']['keywords'],
				'description' => $G['post']['description']
			);
			mysql::update($data,"tag","id='{$id}'");
			alert('操作成功', url::mpf('tag','tag','seo',array('id'=>$id,'success'=>'ok')));
		}else{
			alert('没有提交信息');
		}
	}

	
	public function select()
	{
		global $G;
		$G['cover'] = $this->cover('tag');
		$type = arrExist($G['get'],'type');
		if(is_numeric($type)){
			$res = mysql::select_all('*','tag',"type='{$type}'");
			echo json::encode($res);
		}
	}
	
	 
	public function delete()
	{
		global $G;	
		$this->cover('tag','D');
		if(isset($G['post']['url']) && isset($G['get']['id'])){
			$del = array();
			$arr = explode(',',$G['get']['id']);
			foreach($arr as $id){
				if(is_numeric($id)){
					$del[$id] = $id;
				}
			}
			if($del){
				$error=array();
				foreach($del as $id=>$name){
					if(is_numeric(mysql::delete("tag","id='{$id}'"))){
						
					}else{
						$error[]=$id;
					}
				}
				if($error){
					alert('ID为'.implode(',',$error).'删除失败');
				}else{
					alert('删除成功', url::mpf('tag','tag','init'));
				}
			}else{
				$_booscms;
				alert('没有删除对象id');
			}			
		}
		alert('没有提交信息');
	}
}
?>